<link rel="stylesheet" href="/assets/home/css/ranking.css" />
<!-- 请求 -->
<script src="/assets/home/plugin/axios/axios.min.js"></script>
<script src="/assets/home/plugin/axios/request.js"></script>
<!-- 上拉刷新 -->
<link rel="stylesheet" href="/assets/home/plugin/mescroll/mescroll.min.css" />
<script src="/assets/home/plugin/mescroll/mescroll.min.js"></script>

<div class="top wow">
    <img src="/assets/home/images/ranking.jpg" />
</div>

<div id="box" class="mescroll">
    <div class="list">
       
    </div>
</div>

<!-- 引入底部 -->
{include file='common/footer' /}

<script>

    // 页码
    let page = 1;

    var mescroll = new MeScroll("box", { //第一个参数"mescroll"对应上面布局结构div的id
        //如果您的下拉刷新是重置列表数据,那么down完全可以不用配置,具体用法参考第一个基础案例
        down: {
            callback: downCallback //下拉刷新的回调,别写成downCallback(),多了括号就自动执行方法了
        },
        up: {
            callback: upCallback, //上拉加载的回调
            empty: {
                tip: "暂无相关数据~" //提示
            },
        }
    });

    //下拉刷新的回调
    function downCallback() {
        // 处理方式一: 重置mescroll内部变量(如mescroll.num=1和mescroll.hasNext=true), 自动触发upCallback
        mescroll.resetUpScroll();
        $("#box .list").html('');
    }

    //上拉加载的回调 page = {num:1, size:10}; num:当前页 默认从1开始, size:每页数据条数,默认10
    async function upCallback(val) {
        page = val.num
        let result = await POST({
            url: `{:url('home/index/ranking')}`, params: {
                page: page
            }
        });

        if (result.code == 0) {
            mui.toast(result.msg);
            mescroll.endBySize(0, 0);
            return false;
        }

        let list = result.data.list ?? [];
        let count = result.data.count ?? 0;

        //方法二(推荐): 后台接口有返回列表的总数据量 totalSize
        //必传参数(当前页的数据个数, 总数据量)
        mescroll.endBySize(list.length, count);

        getPageData(list)
    }

    // 页面渲染
    function getPageData(data) {
        if (data.length > 0) {
            data.forEach((item, index) => {
                let str = `
                     <div class="item wow">
                        <div class="content">
                            <div class='box'>
                                <div class="thumb">
                                    <img src="${item.subject ? item.subject.thumbs_text : ''}" />
                                </div>
                                <div class="info">
                                    <div class="name">
                                        <span>课程名称：</span>${item.subject ? item.subject.title : '暂无课程名称'}
                                    </div>
                                    <div class="type">
                                        <span>课程分类：</span>${item.category}
                                    </div>

                                    <div class="teacher">
                                        <span>授课老师：</span>
                                        <a href="{:url('home/teacher/info')}?teacherid=${item.subject ? item.subject.teacherid : 0}">${item.teacher}</a>
                                    </div>

                                    <div class="price">
                                        <span>课程价格：</span>￥${item.subject ? item.subject.price : ''}
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="footer">
                            <div class="left">
                                ${item.count}人购买
                            </div>
                            <div class="right">
                                <a href="{:url('home/subject/info')}?subid=${item.subid}">去购买 ></a>
                            </div>
                        </div>

                        <div class="tip">
                            <span>No.${index + 1}</span>
                        </div>
                    </div>
                `
                $("#box .list").append(str)
            })
        }
    };

</script>